package controllers

import (
	"blog/models"

	"github.com/astaxie/beego"
)

type UpdateController struct {
	beego.Controller
}

func (this *UpdateController) Get() {
	if !checkAccount(this.Ctx) {
		this.Redirect("/admin", 302)
		return
	}
	this.Data["Cookie"] = true
	id := this.GetString("id")
	topic, err := models.GetTopic(id)
	if err != nil {
		beego.Error(err)
		this.Redirect("/", 302)
		return
	}
	this.Data["Topic"] = topic
	this.TplName = "topic_update.html"

}

func (this *UpdateController) Post() {
	if !checkAccount(this.Ctx) {
		this.Redirect("/admin", 302)
		return
	}
	this.Data["Cookie"] = true
	id := this.Input().Get("id")
	text := this.Input().Get("text")
	html := this.Input().Get("html")
	topic_heart := this.Input().Get("topic_heard")

	err := models.UpdateTopic(id, topic_heart, text, html)
	if err != nil {
		beego.Error(err)
	}
	this.Redirect("/", 302)
	return
}

func (this *UpdateController) Delete() {
	if !checkAccount(this.Ctx) {
		this.Redirect("/admin", 302)
		return
	}
	id := this.GetString("id")
	err := models.DeleteTopic(id)
	if err != nil {
		beego.Error(err)
	}
	this.Redirect("/", 302)
	return
}
